package cn.smbms.tools;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 懒汉的单例 用来读取配置文件的单例类
 */
public class ConfigProperties {
    private static ConfigProperties configProperties;
    private Properties params;

    private ConfigProperties() {
        params = new Properties();
        String configFile = "database.properties";
        InputStream is = ConfigProperties.class.getClassLoader().getResourceAsStream(configFile);
        try {
            params.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //3
    public static ConfigProperties getInstance() {//1
        if (configProperties == null) {//
            synchronized (Object.class) {//类对象
                if (configProperties == null) {//双重锁校验单例模式 7种
                    //1
                    configProperties = new ConfigProperties();//2 1
                }
            }
        }
        return configProperties;//单例
    }

    public String getValue(String key) {
        return params.getProperty(key);
    }

}
